package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingsApi;
import com.tanhua.model.domain.Question;
import com.tanhua.model.domain.Settings;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class SettingsService {

    @DubboReference
    private QuestionApi questionApi;

    @DubboReference
    private SettingsApi settingsApi;

    @DubboReference
    private BlackListApi blackListApi;

    // 查询通用设置
    public SettingsVo settings() {
        // 先设置一个vo对象
        SettingsVo vo = new SettingsVo();
        // 1:获取用户的id
        Long userId = UserHolder.getUserId();
        vo.setId(userId);
        // 2:获取用户的手机号码
        vo.setPhone(UserHolder.getMobile());
        // 3:获取用户的陌生人问题
        Question question = questionApi.findByUserId(userId);
        // 3.1:设置一个默认的问题
        String txt = question == null ? "你喜欢动漫吗?" : question.getTxt();
        vo.setStrangerQuestion(txt);
        // 4:获取用户的app通知开关数据
        Settings settings = settingsApi.findByUserId(userId);
        if (settings != null) {
            vo.setGonggaoNotification(settings.getGonggaoNotification());
            vo.setPinglunNotification(settings.getPinglunNotification());
            vo.setLikeNotification(settings.getLikeNotification());
        }
        return vo;
    }

    // 设置陌生人问题
    public void saveQuestion(String content) {
        // 1:获取当前用户id
        Long userId = UserHolder.getUserId();
        // 2:调用api查询当前用户的陌生人问题
        Question question = questionApi.findByUserId(userId);
        // 3:判断问题是否存在
        if (question != null) {
            // 3.1:如果不存在,则保存
            question = new Question();
            question.setUserId(userId);
            question.setTxt(content);
            questionApi.save(question);
        }
        // 3.2:如果存在,则更新
        question.setTxt(content);
        questionApi.update(question);
    }

    // 通知设置
    public void saveSettings(Map map) {
        // 喜欢通知
        boolean likeNotification = (Boolean) map.get("likeNotification");
        // 评论通知
        boolean pinglunNotification = (Boolean) map.get("pinglunNotification");
        // 公告通知
        boolean gonggaoNotification = (Boolean) map.get("gonggaoNotification");

        // 获取当前用户id
        Long userId = UserHolder.getUserId();
        // 根据用户id,查询用户的设置
        Settings settings = settingsApi.findByUserId(userId);
        // 判断?   这一段不是脱裤子放屁?
        if (settings == null) {
            //保存
            settings = new Settings();
            settings.setUserId(userId);
            settings.setPinglunNotification(pinglunNotification);
            settings.setLikeNotification(likeNotification);
            settings.setGonggaoNotification(gonggaoNotification);
            settingsApi.save(settings);
        } else {
            // 修改
            settings.setPinglunNotification(pinglunNotification);
            settings.setLikeNotification(likeNotification);
            settings.setGonggaoNotification(gonggaoNotification);
            settingsApi.update(settings);
        }
    }

    // 分页查询黑名单列表
    public PageResult blacklist(int page, int size) {
        // 1:获取当前用户id
        Long userId = UserHolder.getUserId();
        // 2:调用api查询当前用户的黑名单列表 , IPage对象
        IPage<UserInfo> iPage = blackListApi.findByUserId(userId, page, size);
        // 3:对象转化,将查询的Ipage独享的内容封装到pageResult中去
        PageResult pr = new PageResult(page, size, iPage.getTotal(), iPage.getRecords());
        // 4:返回
        return pr;
    }

    // 取消黑名单
    public void deleteBlackList(Long blackUserId) {
        // 1,获取当前用户id
        Long userId = UserHolder.getUserId();
        // 2,调用api删除
        blackListApi.delete(userId, blackUserId);
    }
}
